package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjWorkTeam;
import com.zb.entity.ZjWorkTeamEmp;
import com.zb.mapper.ZjWorkTeamMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjWorkClientStrategyEmpService;
import com.zb.service.ZjWorkTeamEmpService;
import com.zb.service.ZjWorkTeamService;
import com.zb.vo.WorkTeamEmpVo;
import com.zb.vo.WorkTeamListVo;
import com.zb.vo.WorkTeamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单客服组表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-12
 */
@Service
public class ZjWorkTeamServiceImpl extends ServiceImpl<ZjWorkTeamMapper, ZjWorkTeam> implements ZjWorkTeamService {
    @Resource
    private ZjWorkTeamMapper workTeamMapper;
    @Resource
    private ZjWorkTeamEmpService workTeamEmpService;
    @Resource
    private ZjWorkClientStrategyEmpService workClientStrategyEmpService;

    // 新增或修改工单客服组
    @Override
    @Transactional
    public void save(Long custaccId, WorkTeamVo workTeamVo) {
        Assert.isTrue(null == workTeamMapper.existByName(custaccId, workTeamVo.getId(), workTeamVo.getName()), "客服组名称已重复");
        ZjWorkTeam team = new ZjWorkTeam();
        BeanUtils.copyProperties(workTeamVo, team);
        team.setDisabled(workTeamVo.getStatus());
        if (null != team.getId()) {
            this.updateById(team);
        } else {
            team.setId(IdWorker.getId());
            team.setCustAccId(custaccId);
            team.setEmpId(null == team.getEmpId()? 0L : team.getEmpId());
            this.save(team);
        }
        workTeamEmpService.save(custaccId, team.getId(), workTeamVo.getTeamEmps(), workTeamVo.getDelTeamEmpIds());
    }

    // 根据客服组ID获取客服组详情
    @Override
    public WorkTeamVo getDetail(Long id) {
        ZjWorkTeam team = this.getById(id);
        Assert.isTrue(null != team, "客服组参数[" + id + "]的数据不存在");

        WorkTeamVo vo = new WorkTeamVo();
        BeanUtils.copyProperties(team, vo);
        vo.setStatus(team.getDisabled());
        List<ZjWorkTeamEmp> teamEmps = workTeamEmpService.getByWorkTeamId(id);
        vo.setTeamEmps(teamEmps.stream().map(emp -> {
            return new WorkTeamEmpVo(emp.getId(), emp.getEmpId());
        }).toList());
        return vo;
    }

    // 根据客服组ID集合批量启用或禁用客服组
    @Override
    public void updateDisabledBatchById(List<Long> ids, boolean status) {
        workTeamMapper.updateDisabledBatchById(ids, status);
    }

    // 根据客服组ID集合批量删除客服组
    @Override
    @Transactional
    public void deleteBatchById(Long custAccId, List<Long> ids) {
        List<Long> workTeamIds = workClientStrategyEmpService.getWorkTeamIdByWorkTeamIds(custAccId, ids);
        if (CollectionUtil.isNotEmpty(workTeamIds)) {
            QueryWrapper<ZjWorkTeam> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", workTeamIds);
            wrapper.last("limit 10");
            List<ZjWorkTeam> teamList = this.list(wrapper);
            Assert.isTrue(false, "客服组名称[ " + teamList.stream().map(ZjWorkTeam::getName).collect(Collectors.joining("、")) + " ]已被关联使用，不允许删除");
        }
        workTeamEmpService.deleteBatchByWorkTeamId(ids);
        this.removeByIds(ids);
    }

    // 工单客服组下拉列表查询
    @Override
    public Map<String, Object> workTeamPageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjWorkTeam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjWorkTeam> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = page.getRecords().stream().map(team -> {
            return new SelectResultVo(team.getId(), team.getName());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

}
